from threading import Thread
from flask_mail import Message
from app import mail



def send_async_email(app, msg):
    with app.app_context():
        mail.send(msg)


def send_mail():
    msg = Message('test', sender='1032162439@qq.com',
                  recipients=["1472778078@qq.com"])
    # recipients是个列表，包含所有收件人
    # 此处的test是邮箱的主题，sender和config中的MAIL_USERNAME要一致哦
    msg.body = 'testbody'
    msg.html = '<b>test</b>body'
    # 邮件发送给目标，可以有文本，两种方式呈现，你能看见怎样的取决于你的客户端
    from app.server import app
    thr = Thread(target=send_async_email, args=(app, msg))
    # 使用多线程，在实际开发中，若是不使用异步、多线程等方式，网页会卡住
    thr.start()
    return 'ok'
